package com.lizhe.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * 类描述:  * 自定义Realm
 *
 * @author Lz on 2022-05-18
 */
public class CustomerRealm extends AuthorizingRealm {
    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("========");
        return null;
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        // get username
        String principal = (String) token.getPrincipal();
        System.out.println(principal);

        // DB
        String username = "zhangsan";
        String password = "123456";
        if (username.equals(principal)) {
            return new SimpleAuthenticationInfo(principal, password, this.getName());
        }
        return null;
    }
}
